<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/24
 * @Description: 群组黑名单管理
 */

namespace App\Models;


use App\Http\Controllers\WebApi\Business\ConfigBusiness;
use App\Services\Configs\DefaultService;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Services\Huanxin\GroupServices;
use App\Services\Netease\GroupServices as NetGroupServices;



class ImGroupBlack extends BaseModel
{
    use SoftDeletes;
    protected $table = 'im_group_black';

    /**
     * 关联群组表
     */
    public function group()
    {
        return $this->belongsTo(ImGroup::class, 'im_gid', 'im_gid');
    }


    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'user_id', 'id');
    }

    /**
     * @desc 群组黑名单管理
     * @param string $imGid 群组环信id
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function blackList($imGid, $userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'im_gid', 'user_id')->with(['group' => function($q){
            $q->select('im_gid','name','description','face');
        }])->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['im_gid'=>$imGid])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $list;
    }

    /**
     * @desc 单个群组对群员拉黑
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addSingleBlack($input, $userId = 0)
    {
        $ImGroup = new ImGroup();
        $imGroup = $ImGroup->where('im_gid', $input['im_gid'])->first();
        if (!$imGroup)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        if ($imGroup['user_id'] != $userId)
        {
            return ['code' => 1, 'msg' => '权限不足'];
        }

        $ImGroupUser = new ImGroupUser();
        $imGroupUser = $ImGroupUser->where(['im_gid' => $input['im_gid'], 'user_id' => $input['member_id']])->whereIn('status', [0,3])->first();
        if (!$imGroupUser)
        {
            return ['code' => 1, 'msg' => '操作失败'];
        }

        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $groupServices= new GroupServices();
                $groupServiceInfo = $groupServices->addGroupsBlacklist($input['im_gid'],$input['member_id']);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $groupServiceInfo['code'] = 200;
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $groupServiceInfo['code'] = 200;
                break;
            default:
                $groupServiceInfo['code'] = 400;
                break;
        }

        if ($groupServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '拉黑失败'];
        }

        // 拉黑也就是删除了在群里
        $imGroupUser->status = 2;
        $imGroupUser->deleted_at = time();
        $flag = $imGroupUser->save();

        if (!$flag)
        {
            return ['code' => 1, 'msg' => '拉黑失败'];
        }

        $imGroup->decrement('users_count');

        $this->im_gid = $input['im_gid'];
        $this->user_id = $input['member_id'];
        $this->save();

        return ['code' => 0, 'id' => $this->id, 'msg' => '拉黑成功'];
    }

    /**
     * @desc 批量新增群组黑成员
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addManyBlack($input, $userId = 0)
    {
        $ImGroup = new ImGroup();
        $imGroup = $ImGroup->where('im_gid', $input['im_gid'])->first();
        if (!$imGroup)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        if ($imGroup['user_id'] != $userId)
        {
            return ['code' => 1, 'msg' => '权限不足'];
        }

        $member = $input['member_id'];
        $memberList = explode(",",$member);
        $service['usernames'] = $memberList;
        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $groupServices= new GroupServices();
                $groupServiceInfo = $groupServices->addGroupsBlacklists($input['im_gid'],$service);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $groupServiceInfo['code'] = 200;
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $groupServiceInfo['code'] = 200;
                break;
            default:
                $groupServiceInfo['code'] = 400;
                break;
        }

        if ($groupServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '拉黑失败'];
        }

        // 批量插入拉黑表
        $this->addMoreBlack($member, $input['im_gid']);

        // 改拉黑状态，在给删除，减去人数
        $ImGroupUser = new ImGroupUser();
        $ImGroupUser->where(['im_gid' => $input['im_gid']])->whereIn('user_id', $memberList)->update(["status" => 2, "deleted_at" => time()]);

        $imGroup->decrement('users_count', count($memberList));

        return ['code' => 0, 'id' => $this->id, 'msg' => '拉黑成功'];
    }

    /**
     * @desc 新增多个黑名单
     * @param string $member 逗号隔开的用户id
     * @param string $imGid 群组id
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/24 14:55
     */
    public function addMoreBlack($member, $imGid)
    {
        $memberList = explode(",",$member );
        $groupUser = [];
        foreach ($memberList as $key =>$val)
        {
            $groupUser[$key]['im_gid'] = $imGid;
            $groupUser[$key]['user_id'] = $val;
            $groupUser[$key]['created_at'] = time();
            $groupUser[$key]['updated_at'] = time();
        }

        return $this->insert($groupUser);
    }

    /**
     * @desc 移除单个的群组成员
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function delSingleBlack($input, $userId = 0)
    {
        $ImGroup = new ImGroup();
        $imGroup = $ImGroup->where('im_gid', $input['im_gid'])->first();
        if (!$imGroup)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        if ($imGroup['user_id'] != $userId)
        {
            return ['code' => 1, 'msg' => '权限不足'];
        }
        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $groupServices= new GroupServices();
                $groupServiceInfo = $groupServices->delGroupsBlacklist($input['im_gid'],$input['member_id']);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $groupServiceInfo['code'] = 200;
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $groupServiceInfo['code'] = 200;
                break;
            default:
                $groupServiceInfo['code'] = 400;
                break;
        }

        if ($groupServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '移除失败'];
        }

        $this->where(['im_gid' => $input['im_gid'],'user_id' => $input['member_id']])->delete();

        return ['code' => 0, 'msg' => '移除成功'];

    }

    /**
     * @desc 黑名单批量移除群组成员
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/24 14:58
     */
    public function delManyBlack($input, $userId = 0)
    {
        $ImGroup = new ImGroup();
        $imGroup = $ImGroup->where('im_gid', $input['im_gid'])->first();
        if (!$imGroup)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        if ($imGroup['user_id'] != $userId)
        {
            return ['code' => 1, 'msg' => '权限不足'];
        }

        $member = $input['member_id'];
        $memberList = explode(",",$member);

        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $groupServices= new GroupServices();
                $groupServiceInfo = $groupServices->delGroupsBlacklists($input['im_gid'],$input['member_id']);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $groupServiceInfo['code'] = 200;
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $groupServiceInfo['code'] = 200;
                break;
            default:
                $groupServiceInfo['code'] = 400;
                break;
        }

        if ($groupServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '移除失败'];
        }

        $imGroupBlackUsers = $this->where(['im_gid' => $input['im_gid']])->whereIn('user_id', $memberList)->get();
        if (!$imGroupBlackUsers)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        // 批量删除黑名单就行了，人员拉黑的时候就已经减过了
        $this->where(['im_gid' => $input['im_gid']])->whereIn('user_id', $memberList)->delete();

        return ['code' => 0, 'id' => $this->id, 'msg' => '删除成功'];
    }



}
